public without sharing class ChangeRelatedOwnerHandler {

	public static void changeOwner(List<Account_Owner_Change__c> newList, Map<Id, Account_Owner_Change__c> oldMap) {
		for (Account_Owner_Change__c aoc : newList) {
			Account_Owner_Change__c oldAoc = oldMap.get(aoc.Id);
			if (oldAoc.Status__c != aoc.Status__c && aoc.Status__c == '已批准') {
				// FIXME 审批流只会一条一条执行，所以这里允许在for循环里面写soql
				List<Account> accList = [select Id, OwnerId,IFuserule__c from Account where OwnerChangeProc__c = :aoc.Id];
				Set<Id> accIds = new Set<Id>();
				Set<Id> oldOwnerIds = new Set<Id>();
				for (Account acc : accList) {
					oldOwnerIds.add(acc.OwnerId);
					acc.OwnerId = aoc.Receive_Sale__c;
                    acc.IFuserule__c = !acc.IFuserule__c;///避免进入验证规则
					// 解除关联性
					acc.OwnerChangeProc__c = null;
					accIds.add(acc.Id);
				}
				// 同时把活动的所有人也改了
				List<Event> eList = [select Id from Event where Account__c in :accIds and OwnerId in : oldOwnerIds];
				for (Event e : eList) {
					e.OwnerId = aoc.Receive_Sale__c;
				}

				List<Task> tList = [select Id from Task where Account__c in :accIds and OwnerId in : oldOwnerIds];
				for (Task t : tList) {
					t.OwnerId = aoc.Receive_Sale__c;
				}
				//将业务机会的所有人也修改
                List<Opportunity> oList = [select Id from Opportunity where AccountId in :accIds and OwnerId in :oldOwnerIds and Is_Stop__c != true];
                for(Opportunity o : oList){
                	o.OwnerId = aoc.Receive_Sale__c;
                } 
                //将合同的所有人也修改
                List<Contract__c> cList = [select Id  from Contract__c where TerminalCustomer__c in :accIds  and OwnerId in :oldOwnerIds];
				for(Contract__c c : cList){
                	c.OwnerId = aoc.Receive_Sale__c;
                }
                //将nc客户信用的所有人也修改
                List<AccountCredit__c> acList = [select Id from AccountCredit__c where Account__c in: accIds and OwnerId in: oldOwnerIds];
                for ( AccountCredit__c ac: acList ) {
                	ac.OwnerId = aoc.Receive_Sale__c;
                }
                //将价目表的所有人也修改
                List<PriceList__c> plList = [select Id from PriceList__c  where Account__c in: accIds and OwnerId in: oldOwnerIds];
                for (PriceList__c p : plList) {
                	p.OwnerId = aoc.Receive_Sale__c;
                }

                update accList;
				if (eList.size() > 0) update eList;
				if (tList.size() > 0) update tList;
                if (oList.size() > 0) update oList;
                if (cList.size() > 0) update cList;
                if(acList.size() > 0) update acList;
                if(plList.size() > 0) update plList;
			}
		}
	}
	/*
	public static void releaseProcessLock(List<Account_Owner_Change__c> oldList) {
		List<Id> aocIds = new List<Id>();
		for (Account_Owner_Change__c aoc : oldList) {
			aocIds.add(aoc.Id);
		}
		List<Account> accList = [select Id from Account where OwnerChangeProc__c in :aocIds];
		if (accList.size() > 0) {
			for (Account acc : accList) {
				acc.OwnerChangeProc__c = null;
			}
			update accList;
		}
	}
	*/
}